#include "drv_rtc.h"

uint8_t g_SetRTCEn;

TYPE_RTC g_CurrentRTC;
TYPE_RTC g_UserSetRTC;

void *thread_rtc_read(void *argc)
{
    struct tm t_time;
    time_t timer0;
    struct timeval tv;
    time_t timep;
    while (1)
    {
        if(g_SetRTCEn){
            printf("g_SetRTCEn: %d/%d/%d %d:%d:%d [%d]\r\n",
                    g_UserSetRTC.Year,
                    g_UserSetRTC.Month,
                    g_UserSetRTC.Day,
                    g_UserSetRTC.Hour,
                    g_UserSetRTC.Minute,
                    g_UserSetRTC.Second,
                    g_UserSetRTC.Week);
            t_time.tm_year = g_UserSetRTC.Year + 100;
            t_time.tm_mon = g_UserSetRTC.Month - 1;
            t_time.tm_mday = g_UserSetRTC.Day; 
            t_time.tm_hour = g_UserSetRTC.Hour;
            t_time.tm_min = g_UserSetRTC.Minute;
            t_time.tm_sec = g_UserSetRTC.Second;
            timep = mktime(&t_time);
            g_UserSetRTC.Week = t_time.tm_wday;

            tv.tv_sec = timep;
            tv.tv_usec = 0;
            settimeofday(&tv, (struct timezone *)0);
            system("hwclock -w /dev/rtc0");
            system("hwclock -w /dev/rtc1");
            g_SetRTCEn--;
        }else{
            timer0 = time(NULL);
            struct tm *plocaltime = localtime(&timer0);
            g_CurrentRTC.Second =  plocaltime->tm_sec;                                          
            g_CurrentRTC.Minute =  plocaltime->tm_min;                                      
            g_CurrentRTC.Hour   =  plocaltime->tm_hour;                                      
            g_CurrentRTC.Day    =  plocaltime->tm_mday;                                        
            g_CurrentRTC.Week   =  plocaltime->tm_wday;                                            
            g_CurrentRTC.Month  =  plocaltime->tm_mon + 1;                                          
            g_CurrentRTC.Year   =  plocaltime->tm_year - 100; 
            if((g_CurrentRTC.Year <= 20)||(g_CurrentRTC.Year >= 38)){
                g_CurrentRTC.Second =  0x00;
                g_CurrentRTC.Minute =  0x00;
                g_CurrentRTC.Hour   =  0x08;
                g_CurrentRTC.Day    =  0x01;
                g_CurrentRTC.Week   =  0x01;
                g_CurrentRTC.Month  =  0x01;
                g_CurrentRTC.Year   =  22;
                g_UserSetRTC = g_CurrentRTC;
                g_SetRTCEn = 2;
            }
        }
        // printf("rtc: %d/%d/%d %d:%d:%d [%d]\r\n",
        //          g_CurrentRTC.Year + 2000,
        //          g_CurrentRTC.Month,
        //          g_CurrentRTC.Day,
        //          g_CurrentRTC.Hour,
        //          g_CurrentRTC.Minute,
        //          g_CurrentRTC.Second,
        //          g_CurrentRTC.Week);
        sleep_ms(500);
    }
}


void rtc_start(void)
{
    int res = 0;
    pthread_t tid_rtc_read;

    res = pthread_create(&tid_rtc_read , NULL , thread_rtc_read , NULL);
    if(res){
        perror("thread_rtc_read creat error!\r\n");
    }
}

time_t unix_time_stamp(int year, int mon, int day, int hour, int min, int sec)
{
    struct tm _tm;
    time_t timep;

    _tm.tm_sec = sec;
    _tm.tm_min = min;
    _tm.tm_hour = hour;
    _tm.tm_mday = day;
    _tm.tm_mon = mon - 1;
    _tm.tm_year = year + 100;

    timep = mktime(&_tm);
    return timep;
}